<template>
	<view class="container">
		<tui-tabs :tabs="tabs" :isFixed="scrollTop >= 0" :currentTab="currentTab" selectedColor="#5677fc" sliderBgColor="#5677fc" @change="change" itemWidth="20%"></tui-tabs>
		<!--选项卡逻辑自己实现即可，此处未做处理-->
		<view :class="{ 'tui-order-list': scrollTop >= 0 }">
			<view @click="detail(item)" class="tui-order-item" v-for="(item, orderIndex) in list" :key="orderIndex">
				<tui-list-cell :hover="false" :lineLeft="false">
					<view class="tui-goods-title">
						<view>订单号：{{ item.id }}</view>
						<view v-if="item.orderState == 'waitPay'" class="tui-order-status">待付款</view>
						<view v-if="item.orderState == 'cancel'" class="tui-order-status">已取消</view>
						<view v-if="item.orderState == 'finished'" class="tui-order-status">已完成</view>
						<view v-if="item.orderState == 'waitSend'" class="tui-order-status">待发货</view>
						<view v-if="item.orderState == 'waitAsk'" class="tui-order-status">待收货</view>
					</view>
				</tui-list-cell>
				<block v-for="(items, index) in item.productOrderItemVos" :key="index">
					<tui-list-cell padding="0">
						<view class="tui-goods-item">
							<image :src="items.productCoverUrl" class="tui-goods-img"></image>
							<view class="tui-goods-center">
								<view class="tui-goods-name">{{ items.productName }}</view>
								<view class="tui-goods-attr">{{ items.productSkuName }}</view>
							</view>
							<view class="tui-price-right">
								<view v-if="items.productSkuNum && items.itemAmount">￥{{ (items.itemAmount / items.productSkuNum).toFixed(2) }}</view>
								<view>x{{ items.productSkuNum }}</view>
							</view>
						</view>
					</tui-list-cell>
				</block>
				<tui-list-cell :hover="false" :lineLeft="false">
					<view class="tui-goods-price">
						<!-- 		<view v-if="item.productOrderDiscountVo && item.productOrderDiscountVo.specialOfferAmount" style="margin-left: 20upx;">
							限时优惠：￥{{ item.orderReduceAmount }}
						</view>
						<view v-if="item.productOrderDiscountVo && item.productOrderDiscountVo.integralAmount" style="margin-left: 20upx;">
							积分抵扣：￥{{ item.orderReduceAmount }}
						</view>
						<view v-if="item.productOrderDiscountVo && item.productOrderDiscountVo.vipAmount" style="margin-left: 20upx;">
							会员折扣：￥{{ item.productOrderDiscountVo && item.productOrderDiscountVo.vipAmount }}
						</view> -->
						<view style="margin-left: 20upx;color: #F54F46;">实付金额：￥{{ item.orderRealAmount }}</view>
						<view style="margin-left: 20upx;color: #0BA9FF">商品总额：￥{{ item.orderAmount }}</view>
					</view>
				</tui-list-cell>

				<view v-if="item.orderState === 'waitPay'" class="tui-order-btn">
					<!-- <view class="tui-btn-ml"><tui-button type="black" plain width="152rpx" height="56rpx" :size="26" shape="circle">评价晒单</tui-button></view> -->
					<view class="tui-btn-ml">
						<tui-button @click="handlePay(item)" type="primary" plain width="152rpx" height="56rpx" :size="26" shape="circle">立即支付</tui-button>
					</view>
				</view>
			</view>
		</view>
		<tui-no-data imgUrl="/static/images/toast/img_nodata.png" v-if="!loadding && !list.length">暂无订单</tui-no-data>
		<!--加载loadding-->
		<tui-loadmore v-if="loadding" :index="3" type="red"></tui-loadmore>
		<tui-nomore v-if="!pullUpOn" backgroundColor="#fafafa"></tui-nomore>
		<!--加载loadding-->
	</view>
</template>

<script>
import { orderListPage, orderAlipay } from '@/service/order.js';
export default {
	data() {
		return {
			tabs: [
				{
					name: '待付款',
					type: 'waitPay'
				},
				{
					name: '待发货',
					type: 'waitSend'
				},
				{
					name: '待收货',
					type: 'waitAsk'
				},
				{
					name: '已完成',
					type: 'finished'
				},
				{
					name: '全部',
					type: ''
				}
			],
			currentTab: 0,
			pageNum: 1,
			loadding: false,
			pullUpOn: true,
			scrollTop: 0,
			list: [],
			orderState: ''
		};
	},
	onLoad({ index = 0 }) {
		this.currentTab = Number(index) || 0;
		this.orderState = this.tabs[this.currentTab].type;

		this.getList();
	},
	methods: {
		async handlePay(item) {
			try {
				uni.showLoading();
				const orderDetail = await orderAlipay({ orderId: item.id });
				uni.requestPayment({
					provider: 'alipay',
					orderInfo: orderDetail.body,
					success: e => {
						this.pageNum = 1;
						this.getList();
					},
					fail(e) {
						console.log(e);
					}
				});
			} finally {
				uni.hideLoading();
			}
		},
		async getList() {
			try {
				this.loadding = true;
				const resp = await orderListPage({ orderState: this.orderState, pageNum: this.pageNum, pageSize: 10 });

				if (this.pageNum == 1) {
					this.list = [];
				}
				this.list = this.pageNum == 1 ? resp.data : [...this.list, ...(resp.data || [])];
				this.pageNum++;
			} finally {
				this.loadding = false;
			}
		},
		change(e) {
			this.currentTab = e.index;
			this.orderState = this.tabs[this.currentTab].type;
			this.pageNum = 1;
			this.getList();
		},
		detail(item) {
			uni.navigateTo(
				{
					url: '../orderDetail/orderDetail'
				},
				{
					id: item.id
				}
			);
		}
	},
	async onPullDownRefresh() {
		this.pageNum = 1;
		await this.getList();

		uni.stopPullDownRefresh();
	},
	onReachBottom() {
		this.getList();
	}
};
</script>

<style>
.container {
	padding-bottom: env(safe-area-inset-bottom);
}

.tui-order-list {
	margin-top: 80rpx;
}

.tui-order-item {
	margin-top: 20rpx;
	border-radius: 10rpx;
	overflow: hidden;
}

.tui-goods-title {
	width: 100%;
	font-size: 28rpx;
	display: flex;
	align-items: center;
	justify-content: space-between;
}

.tui-order-status {
	color: #888;
	font-size: 26rpx;
}

.tui-goods-item {
	width: 100%;
	padding: 20rpx 30rpx;
	box-sizing: border-box;
	display: flex;
	justify-content: space-between;
}

.tui-goods-img {
	width: 180rpx;
	height: 180rpx;
	display: block;
	flex-shrink: 0;
}

.tui-goods-center {
	flex: 1;
	padding: 20rpx 8rpx;
	box-sizing: border-box;
}

.tui-goods-name {
	max-width: 310rpx;
	word-break: break-all;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
	font-size: 26rpx;
	line-height: 32rpx;
}

.tui-goods-attr {
	font-size: 22rpx;
	color: #888888;
	line-height: 32rpx;
	padding-top: 20rpx;
	word-break: break-all;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
}

.tui-price-right {
	text-align: right;
	font-size: 24rpx;
	color: #888888;
	line-height: 30rpx;
	padding-top: 20rpx;
}

.tui-color-red {
	color: #5677fc;
	padding-right: 30rpx;
}

.tui-goods-price {
	width: 100%;
	display: flex;
	color: #818181;
	align-items: flex-end;
	justify-content: flex-end;
	font-size: 24rpx;
}

.tui-size-24 {
	font-size: 24rpx;
	line-height: 24rpx;
}

.tui-price-large {
	font-size: 32rpx;
	line-height: 30rpx;
	font-weight: 500;
}

.tui-order-btn {
	width: 100%;
	display: flex;
	align-items: center;
	justify-content: flex-end;
	background: #fff;
	padding: 10rpx 30rpx 20rpx;
	box-sizing: border-box;
}

.tui-btn-ml {
	margin-left: 20rpx;
}
</style>
